package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import entity.FileType;
import entity.User;
import util.DBHelp;
import util.RowMapper;

public class FiletypeDao {

	private DBHelp<FileType> db=new DBHelp<FileType>();
	
	public void save(FileType ft){
		String sql="insert into t_filetype (id,name,createtime,user_id,project_id) values (?,?,?,?,?)";
		db.executeSQL(sql, ft.getId(),ft.getName(),ft.getCreatetime(),ft.getUser_id(),ft.getProject_id());
	}
	
	
	
	public List<FileType> find(){
	
		StringBuilder sql=new StringBuilder();
		sql.append("SELECT t_filetype.id,t_filetype.createtime,username,NAME,project_id,user_id FROM t_filetype" );
		sql.append(" LEFT JOIN  t_user ON t_user.id=t_filetype.user_id ");
		
		
		
		
		return db.executeQueryToList(sql.toString(), new RowMapper<FileType>(){
			
			public FileType mapRow(ResultSet rs) throws SQLException {
				FileType ft=new FileType();
				
				ft.setId(rs.getString("id"));
				ft.setName(rs.getString("name"));
				ft.setProject_id(rs.getString("project_id"));
				ft.setUser_id(rs.getString("user_id"));
				ft.setCreatetime(rs.getString("createtime"));
				
				User user=new User();
				user.setId(rs.getString("user_id"));
				user.setUsername(rs.getString("username"));
			
				ft.setUser(user);
				
			
			
				return ft;
		
			}
			
		});
		
	}
	public class FiletypeRowMapper implements RowMapper<FileType>{

		public FileType mapRow(ResultSet rs) throws SQLException {
			FileType ft=new FileType();
			
			ft.setId(rs.getString("id"));
			ft.setName(rs.getString("name"));
			ft.setProject_id(rs.getString("project_id"));
			ft.setUser_id(rs.getString("user_id"));
			ft.setCreatetime(rs.getString("createtime"));
			
			
		
		
			return ft;
		}
	}
	public List<FileType> findAll() {
		String sql="SELECT id,NAME,createtime,user_id,project_id FROM t_filetype";
		
		return db.executeQueryToList(sql,new FiletypeRowMapper());
	}



	public FileType findById(String id) {
		
		String sql="SELECT id,NAME,createtime,user_id,project_id FROM t_filetype where id=?";
		return db.executeQueryToObject(sql, new FiletypeRowMapper(), id);
	}



	public void delete(String id) {
		String sql="delete from t_filetype where id=?";
		db.executeSQL(sql, id);
	}



	
}
